<?php

namespace app\controller;

use app\model\TorrentBank;
use app\model\TorrentCache;
use support\Request;

use app\helper\ProcessLockHelper;

/**
 * 程序错误结局方案接口
 */
class ErrorController
{

    protected $noNeedLogin = [];

    public function LockClear(Request $request)
    {
        if ($request->method() == "POST") {
            $data = $request->only(['type']);
            try {
                $name = $data['type'] == 1 ? '种子库任务锁' : '辅种任务锁';
                $lock = new ProcessLockHelper($data['type']);
                $lock->removeLock();
                return json(["code" => 200, "msg" => $name . "已删除"]);
            } catch (\Exception $e) {
                return json(["code" => 400, "msg" => $e->getMessage()]);
            }
        }
        return json(["code" => 400, "msg" => "请用POST请求"]);
    }

    public function TorrentBankClear()
    {
        try {
            TorrentBank::where(1, 1)->delete();
            return json(["code" => 200, "msg" => "种子库已清空，等待下次任务自动或手动执行种子库补全"]);
        } catch (\Exception $e) {
            return json(["code" => 400, "msg" => $e->getMessage()]);
        }
    }

    public function TorrentCacheClear(Request $request)
    {
        if ($request->method() == "POST") {
            $data = $request->only(['site_id']);
            try {
                if ($data['site_id'] == 0) {
                    TorrentCache::where(1, 1)->delete();
                } else {
                    TorrentCache::where('sid', $data['site_id'])->delete();
                }
                return json(["code" => 200, "msg" => "缓存已删除"]);
            } catch (\Exception $e) {
                return json(["code" => 400, "msg" => $e->getMessage()]);
            }
        }
        return json(["code" => 400, "msg" => "请用POST请求"]);
    }
}
